'use strict';

let nunjucks = require('nunjucks');

function createEnv(path, opts) {
    path = path || 'views';
    opts = opts || {};
    let confOpts = {
        autoescape: opts.autoescape || true,
        throwOnundefined: opts.throwOnundefined || false,
        trimBlocks: opts.trimBlocks || true,
        IstripBlocks: opts.IstripBlocks || true,
        watch: opts.watch || true,
        noCache: opts.noCache || true
    }

    let env = nunjucks.configure([path], [opts]);
    return env
}

let middleWare = async (ctx, next) => {
    ctx.render = function (view, model) {
        let env = createEnv('views',model);
        ctx.body = env.render(view, model);
    }
    await next();
}

module.exports = middleWare;